/**
 * 自动创建的对应js目录
 * 创建时间：2018-05-18 17:33:14
 * 技术支持：曹传喜
 * 邮箱：15075107120@139.com
 */
$(function () {
    initDatagrid();
});

//初始化表格
function initDatagrid() {
    var columns = [
        {
            field: 'sort',
            title: '优先级',
            align: 'center'
        },
        {
            field: 'property',
            title: '属性名称'
        },
        {
            field: 'type',
            title: '所属分类'
        },
        {
            field: 'column',
            title: '字段名称'
        },
        {
            field: 'remark',
            width: 1,
            title: '备注信息'
        }
    ];
    var type = $.util.getRequest('type');
    var hitField = 'lobbyHit';
    var toolbar = [];
    if (type == 1) {
        toolbar = '#toolbarButton';
    } else {
        var enable = 2;
        var disable = 3;
        if (type == 3) {
            enable = 4;
            disable = 5;
            hitField = 'counterHit';
        }
        columns.push({
            field: hitField,
            title: '状态',
            formatter: function (value) {
                return value == 1 ? '已启用' : '已禁用';
            }
        });
    }
    columns.push({
        field: 'id',
        title: '操作',
        align: 'center',
        formatter: function (value, row) {
            var aEl = '';
            if (type > 1) {
                if (row[hitField] == 1) {
                    aEl = '<a href=javascript:updateStatus("{0}",' + disable + '); class="easyui-tooltip iconImg icon-disable" title="禁用"></a>';
                } else {
                    aEl = '<a href=javascript:updateStatus("{0}",' + enable + '); class="easyui-tooltip iconImg icon-ok" title="启用"></a>';
                }
            } else {
                aEl = '<a href=javascript:edit("{0}","{1}"); class="easyui-tooltip iconImg icon-edit" title="编辑"></a>';
                aEl += '&nbsp;&nbsp;<a href=javascript:updateStatus("{0}",1); class="easyui-tooltip iconImg icon-remove" title="删除"></a>';
            }
            //格式化操作按钮
            return $.string.format(aEl, value, row.typeId);
        }
    });
    $('#toolbarButton').show();
    var url = $.util.getRequestJson('/json/rulescfg.infolist.json');
    var listTable = $('#listTable').datagrid({
        url: url + "?type=" + type,
        idField: 'id',
        fit: true,
        pagination: true,
        pageSize: 20,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [columns]
    });
}

function edit(id, typeId) {
    var title = id ? '编辑客户信息显示设置' : '添加客户信息显示设置';
    var url = $.util.getRootPath() + '/page/rulescfg/showcfg/edit.html';
    url = id ? url + '?id=' + id + '&typeId=' + typeId : url;
    var dialog = $.easyui.showDialog({
        title: title,
        width: 650,
        height: 500,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            subForm(dialog);
        },
        onLoad: function () {
            if (id) {
                var row = $('#listTable').datagrid('findRow', id);
                row.type = row.typeId;
                parent.$('#showCfgForm').form('load', row);
            }
        }
    });
}

function subForm(dialog) {
    var url = $.util.getRootPath() + '/rulescfg/saveinfo';
    var form = parent.$('#showCfgForm').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$(this).form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

function validate() {
    var url = $.util.getRequestJson('/json/rulescfg.validateShow.json');
    var param = {column: parent.$('#column').val(), sql: parent.$('#sql').val()};
    $.util.post(url, param, function (json) {
        if (json.success) {
            $.easyui.messager.show("测试通过！");
        } else {
            $.easyui.messager.show(json.message);
        }
    });
}

function updateStatus(id, status) {
    var msg = '确定要删除吗？';
    if (status == 2 || status == 4) {
        msg = '确定要启用吗？';
    } else if (status == 3 || status == 5) {
        msg = '确定要禁用吗？';
    }
    $.easyui.messager.confirm(msg, function (r) {
        if (r) {
            var url = $.util.getRequestJson('/json/rulescfg.updateCfg.json');
            var param = {id: id, type: status};
            $.util.post(url, param, function (json) {
                $('#listTable').datagrid('reload');
                $.easyui.messager.show(json.message);
            });
        }
    });
}

